#include "StdAfx.h"
#include "Parser.h"


Parser::Parser(void)
{
}


Parser::~Parser(void)
{
}

LRESULT Parser::FindElementValueInternal(
	const std::wstring& src,const std::wstring& startTag,const std::wstring& endTag ,std::wstring& result)
{
	std::wstring::size_type pos1 = std::wstring::npos;
	std::wstring::size_type pos2 = std::wstring::npos;
	pos1 = src.find(startTag);
	pos2 = src.find(endTag);
	if(pos1 == std::wstring::npos)
		return -1;
	if(pos2 == std::wstring::npos)
		return -1;
	std::wstring::size_type start = pos1 + startTag.length();
	result =  src.substr( start , pos2 - start);
	return 0;
}

LRESULT Parser::FindElementAttribute(
	const std::wstring& src,const std::wstring& elementName,const std::wstring& attributeName,std::wstring& result)
{
	std::wstring::size_type pos1 = std::wstring::npos;
	std::wstring::size_type pos2 = std::wstring::npos;
	std::wstring::size_type posquto1 = std::wstring::npos;
	std::wstring::size_type posquto2 = std::wstring::npos;
	pos1 = src.find(elementName);
	if(pos1 == std::wstring::npos)
		return -1;
	pos2 = src.find(attributeName,pos1);
	if(pos2 == std::wstring::npos)
		return -1;
	posquto1 = src.find('"',pos2);
	if(posquto1 == std::wstring::npos)
		return -1;
	posquto2 = src.find('"',posquto1 + 1);
	if(posquto2 == std::wstring::npos)
		return -1;
	if(posquto2 == posquto1 + 1){
		result = L"";
		return 0;
	}
	result = src.substr(posquto1+1,posquto2-posquto1-1);
	return 0;
}

LRESULT Parser::FindServiceProvider(const std::wstring& src , std::wstring& result)
{
	std::wstring elementStartTag(L"<serviceProvider>");
	std::wstring elementEndTag(L"</serviceProvider>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindServiceUri(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<serviceUri>");
	std::wstring elementEndTag(L"</serviceUri>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindSignUpPage(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<signUpPage>");
	std::wstring elementEndTag(L"</signUpPage>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindLocalName(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<localName>");
	std::wstring elementEndTag(L"</localName>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindSMSmaxRecipientsPerMessage(const std::wstring& src, std::wstring& result)
{
	std::wstring elementStartTag(L"<SMS_SENDER");
	std::wstring elementAttribute(L"maxRecipientsPerMessage");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}
LRESULT Parser::FindSMSmaxMessagesPerSend(const std::wstring& src, std::wstring& result)
{
	std::wstring elementStartTag(L"<SMS_SENDER");
	std::wstring elementAttribute(L"maxMessagesPerSend");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}
LRESULT Parser::FindSMSmaxSbcsPerMessage(const std::wstring& src, std::wstring& result)
{
	std::wstring elementStartTag(L"<SMS_SENDER");
	std::wstring elementAttribute(L"maxSbcsPerMessage");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}
LRESULT Parser::FindSMSmaxDbcsPerMessage(const std::wstring& src, std::wstring& result)
{
	std::wstring elementStartTag(L"<SMS_SENDER");
	std::wstring elementAttribute(L"maxDbcsPerMessage");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}

LRESULT Parser::FindUserInfoErrorCode(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<error");
	std::wstring elementAttribute(L"code");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}

LRESULT Parser::FindUserReplyPhone(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<replyPhone>");
	std::wstring elementEndTag(L"</replyPhone>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindUserSmtpAddress(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<smtpAddress>");
	std::wstring elementEndTag(L"</smtpAddress>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}

LRESULT Parser::FindSendResponseErrorCode(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<error");
	std::wstring elementAttribute(L"code");
	return FindElementAttribute(src,elementStartTag,elementAttribute,result);
}

LRESULT Parser::FindSendResponseUnreachedReceiver(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<recipientList>");
	std::wstring elementEndTag(L"</recipientList>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}


LRESULT Parser::FindSendResponseUnreachedContent(const std::wstring& src,std::wstring& result)
{
	std::wstring elementStartTag(L"<content>");
	std::wstring elementEndTag(L"</content>");
	return FindElementValueInternal(src,elementStartTag,elementEndTag,result);
}